package it.slkfarm.drawable;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.Shape;

public class CustomShapeDrawable extends ShapeDrawable
{
	/**
	 * @uml.property  name="fillPaint"
	 * @uml.associationEnd  multiplicity="(1 1)"
	 */
	private final Paint fillPaint;
	/**
	 * @uml.property  name="strokePaint"
	 * @uml.associationEnd  multiplicity="(1 1)"
	 */
	private final Paint strokePaint;
	/**
	 * @uml.property  name="level"
	 */
	private int level;
	private static final String UNDER_SUPPLY_0_11 = "#82FA58";
	private static final String UNDER_SUPPLY_12_22 = "#40FF00";
	private static final String UNDER_SUPPLY_23_33 = "#31B404";
	
	private static final String NORMAL_SUPPLY_34_45 = "#F7D358";
	private static final String NORMAL_SUPPLY_46_57 = "#FFBF00";
	private static final String NORMAL_SUPPLY_58_67 = "#B18904";
	
	private static final String OVER_SUPPLY_68_79 = "#FE2E2E";
	private static final String OVER_SUPPLY_80_91 = "#B40404";
	private static final String OVER_SUPPLY_92_100 = "#610B0B";
	
	private static final String DISABLED = "#BDBDBD";
	private static final String BASIC = "#43520B";
	private static final String BACKGROUND = "#FFF5F5F5";
	/**
	 * @uml.property  name="strokeWidth"
	 */
	private int strokeWidth;
	
	
	public CustomShapeDrawable(Shape shape, double level, int width, boolean disabled)
	{
		super(shape);
		strokeWidth = width;
		int color;
		if (!disabled)
			color = getColor(level);
		else
			color = Color.parseColor(DISABLED); 
		fillPaint = new Paint(this.getPaint());
		//fillPaint.setColor(Color.TRANSPARENT);
		fillPaint.setColor(Color.parseColor(BACKGROUND));
        strokePaint = new Paint(fillPaint);
        strokePaint.setStyle(Paint.Style.STROKE);
        strokePaint.setStrokeWidth(strokeWidth);
        strokePaint.setColor(color);      
	}
	
	@Override
    protected void onDraw(Shape shape, Canvas canvas, Paint paint) 
	{		
		shape.resize(canvas.getClipBounds().right, canvas.getClipBounds().bottom);
	    shape.draw(canvas, fillPaint);
	    Matrix matrix = new Matrix();
	    matrix.setRectToRect(new RectF(0, 0, canvas.getClipBounds().right, canvas.getClipBounds().bottom),
	            new RectF(strokeWidth/2, strokeWidth/2, canvas.getClipBounds().right - strokeWidth/2, canvas.getClipBounds().bottom - strokeWidth/2), Matrix.ScaleToFit.FILL);
	    canvas.concat(matrix);
	    shape.draw(canvas, strokePaint);
	    
    }
	
	private int getColor(double level)
	{
		int colorCode = 0;
		Color color = new Color();
		if (level<12)
			colorCode = Color.parseColor(UNDER_SUPPLY_0_11);
		else if (level>=12 && level<=22)
			colorCode = Color.parseColor(UNDER_SUPPLY_12_22);
		else if (level>=23 && level<=33)
			colorCode = Color.parseColor(UNDER_SUPPLY_23_33);
		else if (level>=34 && level<=45)
			colorCode = Color.parseColor(NORMAL_SUPPLY_34_45);
		else if (level>=46 && level<=57)
			colorCode = Color.parseColor(NORMAL_SUPPLY_46_57);
		else if (level>=58 && level<=67)
			colorCode = Color.parseColor(NORMAL_SUPPLY_58_67);
		else if (level>=68 && level<=79)
			colorCode = Color.parseColor(OVER_SUPPLY_68_79);
		else if (level>=80 && level<=91)
			colorCode = Color.parseColor(OVER_SUPPLY_80_91);
		else if (level>=92)
			colorCode = Color.parseColor(OVER_SUPPLY_92_100);
		return colorCode;
	}
}
